<%--
  Created by IntelliJ IDEA.
  User: User
  Date: 2020/8/25
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>进销存后台管理登陆</title>
    <script src="${pageContext.request.contextPath}/static/jquery-3.5.1.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <link href="${pageContext.request.contextPath}/static/toastr-master/toastr.min.css" rel="stylesheet"/>
    <script src="${pageContext.request.contextPath}/static/toastr-master/toastr.min.js"></script>
</head>
<style>
    #content {
        width: 400px;
        height: 250px;
        margin: 15% 30%;
        opacity: 0.85;
    }

    body {
        background-image: url("/static/img/logtwo.jpg");
        background-size: 100%;
        background-repeat: no-repeat;
    }
</style>
<body>
<div class="layui-card" id="content">
    <div class="layui-card-header">进销存后台管理登陆</div>
    <div class="layui-card-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="staffId" lay-verify="username" placeholder="请输入用户名"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" lay-verify="pass" placeholder="请输入密码"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit lay-filter="index">登录</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    /*表单验证*/
    layui.use('form', function () {
        var form = layui.form;
        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
            }
            //我们既支持上述函数式的方式，也支持下述数组的形式
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
        });
        form.on('submit(index)', function (data) {
            $.ajax({
                url: '/staffServlet/checkLogin',
                type: 'post',
                dataType: 'json',
                data: data.field,
                success: function (result) {
                    let resultValue = Number(result)
                    if (resultValue === 666) {
                        window.location.href = '/menuServlet/getAllMenuByRid';
                    } else if (resultValue === 1) {
                        toastr.error("您已经离职，无法登录")
                    } else if (resultValue === 0) {
                        toastr.error("您的账号或密码错误，请仔细检查！！")
                    } else {
                        window.location.href = '/index.jsp';
                    }
                }
            })
        })
    });
</script>

</html>